package com.sos.core.game
{
	import com.sos.utils.ClassUtils;

	public class GameServerStatus
	{
			
		// -------------------------------------------------------------------------------
		// CONSTANTS
		// -------------------------------------------------------------------------------              
		
		public static const UNKNOWN:GameServerStatus				= new GameServerStatus(0, 	"UNKNOWN",				"Game status unknown");									// 
		public static const WAITING:GameServerStatus				= new GameServerStatus(1, 	"WAITING",				"Server is waiting for player PLAY request");			// GamePhase.PLAY
		public static const CALCULATING:GameServerStatus			= new GameServerStatus(2, 	"CALCULATING",			"Server is waiting for opponent PLAY request");			// GamePhase.RESOLVE_PLAY (polling)
		public static const COMPLETED:GameServerStatus				= new GameServerStatus(3, 	"COMPLETED",			"Game completed if last turn, RESYNC otherwise");		// GamePhase.RESYNC or game end
		public static const DISCARD:GameServerStatus				= new GameServerStatus(4, 	"DISCARD",				"Server is waiting for player DISCARD request");		// GamePhase.DISCARD
		public static const SUBSTITUTION:GameServerStatus			= new GameServerStatus(5, 	"SUBSTITUTION",			"Server is waiting for player SUBSTITUTE request");		// GamePhase.SUBSTITUTE
		public static const SOLVE_LOCK:GameServerStatus				= new GameServerStatus(6, 	"SOLVE_LOCK",			"Server PLAY calc lock");								// GamePhase.RESOLVE_PLAY (polling)
		public static const DISCARD_POLLING:GameServerStatus		= new GameServerStatus(7, 	"DISCARD_POLLING",		"Server is waiting for opponent DISCARD request");		// GamePhase.RESOLVE_DISCARD (polling)
		public static const DISCARD_LOCK:GameServerStatus			= new GameServerStatus(8, 	"DISCARD_LOCK",			"Server DISCARD calc lock");							// GamePhase.RESOLVE_PLAY (polling)
		public static const SUBSTITUTION_POLLING:GameServerStatus	= new GameServerStatus(9, 	"SUBSTITUTION_POLLING",	"Server is waiting for opponent SUBSTITUTION request");	// GamePhase.RESOLVE_DISCARD (polling)
		public static const SUBSTITUTION_LOCK:GameServerStatus		= new GameServerStatus(10, 	"SUBSTITUTION_LOCK",	"Server SUBSTITUTION calc lock");						// GamePhase.RESOLVE_PLAY (polling)
		
		// -------------------------------------------------------------------------------
		// STATIC
		// -------------------------------------------------------------------------------
		
		private static var _initialized:Boolean = false;
		private static var _list:Vector.<GameServerStatus>;
		
		// Fake static constructor
		{
			// Fill the internal list
			_list = new Vector.<GameServerStatus>();
			_list.push(GameServerStatus.UNKNOWN);
			_list.push(GameServerStatus.WAITING);
			_list.push(GameServerStatus.CALCULATING);
			_list.push(GameServerStatus.COMPLETED);
			_list.push(GameServerStatus.DISCARD);
			_list.push(GameServerStatus.SUBSTITUTION);
			_list.push(GameServerStatus.SOLVE_LOCK);
			_list.push(GameServerStatus.DISCARD_POLLING);
			_list.push(GameServerStatus.DISCARD_LOCK);
			_list.push(GameServerStatus.SUBSTITUTION_LOCK);
			// Init!
			_initialized = true;
		}
		
		// -------------------------------------------------------------------------------
		// FIELDS
		// -------------------------------------------------------------------------------
		
		private var _id:uint;
		private var _name:String;
		private var _description:String;
		
		// -------------------------------------------------------------------------------
		// CONSTRUCTORS
		// -------------------------------------------------------------------------------
		
		public function GameServerStatus(id:uint, name:String, desc:String)
		{
			// Safe unique instance lock
			if (_initialized) throw new Error("GameServerStatus enum already created!");
			// Init
			_id    			= id;
			_name  			= name;
			_description	= desc;
		}

		// -------------------------------------------------------------------------------
		// PROPERTIES
		// -------------------------------------------------------------------------------              
		
		public function get id():uint
		{
			return _id;
		}
		
		public function get name():String
		{
			return _name;
		}
		
		public function get description():String
		{
			return _description;
		}
		
		// -------------------------------------------------------------------------------
		// PUBLIC METHODS
		// -------------------------------------------------------------------------------
		
		public function toString():String
		{
			return ClassUtils.getName(this) + "." + _name;
		}
		
		// -------------------------------------------------------------------------------
		// STATIC METHODS
		// -------------------------------------------------------------------------------
		
		/**
		 * Returns the list of the names of 
		 * @return 
		 */
		public static function names():Array
		{
			var list:Array  = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i].name);
			}
			return list;
		}
		
		/**
		 * 
		 * @return 
		 */
		public static function list():Array
		{
			var list:Array = new Array;
			// Loop over values list
			for (var i:int=0; i<_list.length; i++)
			{
				list.push(_list[i]);
			}
			return list;
		}
				
		/**
		 * Search a GameServerStatus by given id 
		 * @param id Id of the card type to find
		 * @return PlayerGameServerStatus constant for the given id, null if no GameServerStatus
		 * for the given id has been found
		 */             
		public static function getById(id:uint):GameServerStatus
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].id == id) return _list[i];
			}
			throw new Error("GameServerStatus for id '" + id + "' unknown!");
		}
		
		/**
		 * Search a GameServerStatus by given name 
		 * @param name Name of the player GameServerStatus to find
		 * @return PlayerGameServerStatus constant for the given id, null if no GameServerStatus
		 * for the given name has been found
		 */             
		public static function getByName(name:String):GameServerStatus
		{
			// Search the value into the list
			for (var i:int=0; i<_list.length; i++)
			{
				if (_list[i].name == name) return _list[i];
			}
			throw new Error("GameServerStatus for name '" + name + "' unknown!");
		}
		
	}
}